﻿#include "GreedyFood.h"

#include <windows.h>

void gotoxy2(HANDLE hOut2, int x, int y)
{
	COORD pos;
	pos.X = x;             //横坐标
	pos.Y = y;            //纵坐标
	SetConsoleCursorPosition(hOut2, pos);
}
HANDLE hOut2 = GetStdHandle(STD_OUTPUT_HANDLE);//定义显示器句柄变量

GreedyFood::GreedyFood(GreedyWall& tempWall) :wall(tempWall)
{

}

void GreedyFood::setFood()
{
	while (true)
	{
		foodX = rand() % (GreedyWall::ROW - 2) + 1;
		foodY = rand() % (GreedyWall::COL - 2) + 1;

		//1.如果随机的位置是蛇头或者蛇身，就重新生成随机数；
		if (wall.getWall(foodX, foodY) == ' ')
		{
			wall.setWall(foodX, foodY, '#');

			gotoxy2(hOut2, foodY * 2, foodX);
			cout << "#";
			break;
		}
	}
}
